package com.dodo.blog.model;

import com.comvai.auth.model.AuthUser;
import com.comvai.auth.model.LoginProvider;
import com.comvai.auth.model.LoginType;
import com.dodo.blog.model.groups.ClassicLoginTypeGroup;
import com.dodo.blog.model.groups.EditAccountGroup;
import com.dodo.blog.model.groups.OpenIdLoginTypeGroup;
import org.codehaus.jackson.annotate.JsonIgnore;

import javax.persistence.Entity;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
@Entity
public class Account
        extends BaseEntity
        implements AuthUser
{
    private static final long serialVersionUID = 1L;

    @NotNull( groups = {OpenIdLoginTypeGroup.class} )
    private String email;

    @NotNull( groups = {ClassicLoginTypeGroup.class} )
    private String login;

    @NotNull( groups = {ClassicLoginTypeGroup.class} )
    private String password;

    @NotNull( groups = {ClassicLoginTypeGroup.class} )
    @Transient
    private String passwordAgain;

    @NotNull( groups = EditAccountGroup.class )
    private LoginType loginType;

    @NotNull( groups = EditAccountGroup.class )
    private LoginProvider loginProvider;

    @NotNull( groups = EditAccountGroup.class )
    private String name;

    @NotNull( groups = EditAccountGroup.class )
    private String surname;

    @NotNull( groups = EditAccountGroup.class )
    private Boolean notify;

    private String avatarBlobKey;

    private String contactEmail;

    @NotNull( groups = EditAccountGroup.class )
    private Role role;

    private Integer loginAttempts = 0;

    private Boolean locked;

    private String lockedReason;

    private Date lockedDate;

    @Transient
    private String userInfo;

    public String getEmail()
    {
        return email;
    }

    public void setEmail( String email )
    {
        this.email = email;
    }

    public String getLogin()
    {
        return login;
    }

    public void setLogin( String login )
    {
        this.login = login;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword( String password )
    {
        this.password = password;
    }

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public String getSurname()
    {
        return surname;
    }

    public void setSurname( String surname )
    {
        this.surname = surname;
    }

    public Boolean getNotify()
    {
        return notify;
    }

    public void setNotify( Boolean notify )
    {
        this.notify = notify;
    }

    public String getAvatarBlobKey()
    {
        return avatarBlobKey;
    }

    public void setAvatarBlobKey( String avatarBlobKey )
    {
        this.avatarBlobKey = avatarBlobKey;
    }

    public Role getRole()
    {
        return role;
    }

    public void setRole( Role role )
    {
        this.role = role;
    }

    public Integer getLoginAttempts()
    {
        return loginAttempts;
    }

    public void setLoginAttempts( Integer loginAttempts )
    {
        this.loginAttempts = loginAttempts;
    }

    public Boolean getLocked()
    {
        return locked;
    }

    public void setLocked( Boolean locked )
    {
        this.locked = locked;
    }

    public String getContactEmail()
    {
        return contactEmail;
    }

    public void setContactEmail( String contactEmail )
    {
        this.contactEmail = contactEmail;
    }

    public String getLockedReason()
    {
        return lockedReason;
    }

    public void setLockedReason( String lockedReason )
    {
        this.lockedReason = lockedReason;
    }

    public Date getLockedDate()
    {
        return lockedDate;
    }

    public void setLockedDate( Date lockedDate )
    {
        this.lockedDate = lockedDate;
    }

    @Override
    public LoginType getLoginType()
    {
        return loginType;
    }

    @Override
    public LoginProvider getLoginProvider()
    {
        return loginProvider;
    }

    public void setLoginType( LoginType loginType )
    {
        this.loginType = loginType;
    }

    public void setLoginProvider( LoginProvider loginProvider )
    {
        this.loginProvider = loginProvider;
    }

    public String getPasswordAgain()
    {
        return passwordAgain;
    }

    public void setPasswordAgain( String passwordAgain )
    {
        this.passwordAgain = passwordAgain;
    }

    @Override
    public String getUserId()
    {
        return getId() != null ? getId().toString() : null;
    }

    @Override
    public String getUserInfo()
    {
        if ( loginType != null )
        {
            switch ( loginType )
            {
                case OPEN_ID:
                case OAUTH:
                {
                    return email;
                }
                case LOGIN:
                {
                    return password;
                }
            }
        }

        return null;
    }

    @JsonIgnore
    public void setUserInfo( String userInfo )
    {
        this.userInfo = userInfo;
    }
}